// pages/order_coupons/order_coupons.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listArr: [],
    isUseLineHidden: false,
    isPastDueLineHidden: true,
    clickNum: 1,
    // 已选图片
    selectedImage: '/images/coupons_orderSelected.png',
    // 不可用图片
    unusedImage: '/images/coupons_unused.png',
    noDataImage: '/images/coupon_nodata.png',
    // 可用按钮字体颜色 
    userVSelectedColor: "#34e5ea",
    // 不可用按钮字体颜色
    pastDueVSelectedColor: "#666666",
    state: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var userName = options.username
    var priceTotal = options.priceTotal
    var scId = options.scId
    this.setData({
      userName: userName,
      priceTotal: priceTotal,
      scId: scId
    })
    that.loadData(userName, priceTotal)
  },
  // 加载数据
  loadData(username, totalPrice) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.baseNet + 'coupon/queryCouponInfoDetails?',
      data: {
        username: username,
        totalPrice: totalPrice
      },
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res.data)
        wx.hideLoading()
        if (res.data.messcode == '001') {
          let num = that.data.clickNum
          if (num == 1) { //可用优惠劵
            that.setData({
              listArr: res.data.normalCoupon,
              messcode: '001'
            })
          } else if (num == 2) {
            that.setData({
              listArr: res.data.overdueCoupon,
              messcode: '001'
            })
          }
        } else if (res.data.messcode == '002') {
          // 没有数据、
          that.setData({
            messcode: '002'
          })
        } else if (res.data.messcode == '003') {
          // 服务器异常
          that.setData({
            messcode: '003'
          })
        }
      },
    })
  },
  // 点击可用优惠券按钮
  useClick: function() {
    var that = this;
    that.setData({
      isUseLineHidden: false,
      isPastDueLineHidden: true,
      userVSelectedColor: '#34e5ea',
      pastDueVSelectedColor: '#666666',
      clickNum: 1,
      state: 1
    })
    that.loadData(this.data.userName, this.data.priceTotal)
  },
  // 点击不可用优惠券按钮
  pastDueClick: function() {
    var that = this;
    that.setData({
      isUseLineHidden: true,
      isPastDueLineHidden: false,
      userVSelectedColor: '#666666',
      pastDueVSelectedColor: '#34e5ea',
      clickNum: 2,
      state: 2
    })
    that.loadData(this.data.userName, this.data.priceTotal)
  },
  // 选择可使用按钮的优惠券
  itemClick: function(e) {
    if (this.data.state == 1) {
      console.log(e)
      var that = this
      var index = e.currentTarget.dataset.id
      var scId = this.data.listArr[index].scId
      var couponPrice = this.data.listArr[index].price
      var pages = getCurrentPages()
      var curPage = pages[pages.length - 1]
      var prePage = pages[pages.length - 2]
      prePage.setData({
        scId: scId,
        couponPrice: couponPrice,
        acturalPrice: couponPrice == null ? that.data.priceTotal : that.data.priceTotal - couponPrice
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})